package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private SubCommentMapper subCommentMapper;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TagMapper tagMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public StrategyComment selectByPrimaryKey(Long id) {
        return strategyCommentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyComment> selectAll() {
        return null;
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {

    }

    @Override
    public PageInfo getList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyComment> list = strategyCommentMapper.getList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<StrategyComment> getCommentByStrategyId(Long id) {
        List<StrategyComment> comments = strategyCommentMapper.getCommentByStrategyId(id);
        return comments;
    }


    @Override
    public void insertSubComment(SubComment subComment) {
        System.out.println(subComment);
        subCommentMapper.insert(subComment);
    }

    @Override
    public List<SubComment> getSubCommentById(Long id) {
        return subCommentMapper.selectById(id);
    }

    @Override
    public List<SubComment> getSubContentByUserId(Long id) {

        return  subCommentMapper.getSubContentByUserId(id);
    }
    @Override
    public List<StrategyComment> selectCommentByState() {
        List<StrategyComment> strategyComments = strategyCommentMapper.selectCommentByState();
        for (StrategyComment strategyComment : strategyComments) {
            Strategy strategy = strategyMapper.selectByPrimaryKey(strategyComment.getStrategy().getId());
            strategyComment.setStrategy(strategy);
            User user = userMapper.selectByPrimaryKey(strategyComment.getUser().getId());
            strategyComment.setUser(user);
        }
        return  strategyComments;
    }

    @Override
    public List<StrategyComment> selectByMentions(String keyword) {
        return strategyCommentMapper.selectByMentions(keyword);
    }

    //添加评论和标签
    @Override
    public void addComment(StrategyComment comment, String tags) {
        comment.setCreateTime(new Date());
        comment.setHasRead(0);
        strategyCommentMapper.insert(comment);
        String[] split = tags.split(";");
        for (String s : split) {
            if (s.length() != 0) {
                Tag tag = new Tag();
                tag.setName(s);
                tagMapper.insert(tag);
            }

        }
    }

    @Override
    public List<StrategyComment> selectByAuthorId(Long id) {
        return strategyCommentMapper.selectByAuthorId(id);
    }

    //获取子未读的子评论数量
    @Override
    public int getCountHasReadComment(Long id) {
        return subCommentMapper.getCountHasReadComment(id);
    }

    @Override
    public void setSubHasRead(Map<String, Object> map) {
        subCommentMapper.setHasRead(map);
    }

    @Override
    public Integer getNoReadMentions(String keyword, Integer hasRead) {

        return strategyCommentMapper.getNoReadMentions(keyword,hasRead);
    }

    @Override
    public void setCommentsHasRead(String keyword) {
        strategyCommentMapper.setCommentsHasRead(keyword);
    }

    @Override
    public int getCount(Long id) {
        return strategyCommentMapper.getCount(id);
    }

}
